<?php
/**
 * Joomla! 1.5 component ArticleCashRewards
 *
 * @version $Id: config.php 2009-05-10 01:47:01 svn $
 * @author Maverick
 * @package models
 * @subpackage ArticleCashRewards
 * @license GNU/GPL
 * Website: http://www.boddunan.com
 *
 * Review and approve the articles and assign points and cash rewards.
 * Users can see their articles and the points/cash rewards assigned in the front-end module.
 */

// no direct access
defined('_JEXEC') or die('Restricted access');

// Import Joomla! libraries
jimport('joomla.application.component.model');

if(!class_exists('ContentHelperRoute')) require_once (JPATH_SITE . '/components/com_content/helpers/route.php');  

class ArticleCashRewardsModelConfig extends JModel {

	var $_total = null; 
	var $_pagination = null;
    var $_error = null;
	
	function __construct(){
	
		parent::__construct();
		global $mainframe, $option;
	}

    function saveConfiguration() {
	    
    	global $mainframe, $option;
    	
    	// Get a database object
		$db     =& JFactory::getDBO();
        //$data   = JRequest::get( 'post' );

		$min_pay_limit          = JRequest::getVar(CONFIG_MIN_PAYMENT_THRESHOLD, 0,'post','INTEGER');
//		$payment_cutoff_day     = JRequest::getVar(CONFIG_PAYMENT_CUTOFF_DAY, 15,'post','INTEGER');
        $default_pay_method     = JRequest::getVar(CONFIG_DEFAULT_PAY_METHOD, 'Check','post','STRING');
        $enable_points          = JRequest::getVar(CONFIG_ENABLE_POINTS, '0','post','INTEGER');
        $enable_weblinks        = JRequest::getVar(CONFIG_ENABLE_WEBLINKS, '0','post','INTEGER');
        $enable_referrals       = JRequest::getVar(CONFIG_ENABLE_REFERRALS, '0','post','INTEGER');
        $enable_awards          = JRequest::getVar(CONFIG_ENABLE_AWARDS, '0','post','INTEGER');
        $weblink_cash_default   = JRequest::getVar(CONFIG_WEBLINK_CASH_CREDIT, '0','post','INTEGER');
        $sender_email           = JRequest::getVar(CONFIG_SENDER_EMAIL, '','post','STRING');
        $sender_name            = JRequest::getVar(CONFIG_SENDER_NAME, '','post','STRING');
        $a_article_email_sub    = JRequest::getVar(CONFIG_A_ARTICLE_EMAIL_SUB, '','post','STRING');
        $a_article_email_body   = JRequest::getVar(CONFIG_A_ARTICLE_EMAIL_BODY, '','post','STRING');
        $r_article_email_sub    = JRequest::getVar(CONFIG_R_ARTICLE_EMAIL_SUB, '','post','STRING');
        $r_article_email_body   = JRequest::getVar(CONFIG_R_ARTICLE_EMAIL_BODY, '','post','STRING');
        $referral_system        = JRequest::getVar(CONFIG_REFERRAL_SYSTEM, '','post','STRING');
        $points_system          = JRequest::getVar(CONFIG_POINTS_SYSTEM, '','post','STRING');
        $jomsocial_ratings      = JRequest::getVar(CONFIG_JS_POINTS_RATINGS, '','post','INTEGER');
        $default_form_pos       = JRequest::getVar(CONFIG_REVIEW_FORM_POSITION, REVIEW_FORM_POSITION_BOTTOM,'post','STRING');
        $default_review_status  = JRequest::getVar(CONFIG_REVIEW_STATUS, STATUS_APPROVED,'post','STRING');
        
        $query = 'INSERT INTO ' . TABLE_ACR_CONFIG . ' (config_name, config_value) VALUES' .
            ' ("' . CONFIG_MIN_PAYMENT_THRESHOLD . '","' . $min_pay_limit . '"),'.
//            ' ("' . CONFIG_PAYMENT_CUTOFF_DAY . '","' . $payment_cutoff_day . '"),'.
            ' ("' . CONFIG_DEFAULT_PAY_METHOD . '","' . $default_pay_method . '"),'.
            ' ("' . CONFIG_ENABLE_POINTS . '","' . $enable_points . '"),'.
            ' ("' . CONFIG_ENABLE_WEBLINKS . '","' . $enable_weblinks . '"),'.
            ' ("' . CONFIG_ENABLE_REFERRALS . '","' . $enable_referrals . '"),'.
            ' ("' . CONFIG_ENABLE_AWARDS . '","' . $enable_awards . '"),'.
            ' ("' . CONFIG_WEBLINK_CASH_CREDIT . '","' . $weblink_cash_default . '"),'.
            ' ("' . CONFIG_SENDER_EMAIL . '","' . $sender_email . '"),'.
            ' ("' . CONFIG_SENDER_NAME . '","' . $sender_name . '"),'.
            ' ("' . CONFIG_A_ARTICLE_EMAIL_SUB . '","' . $a_article_email_sub . '"),'.
            ' ("' . CONFIG_A_ARTICLE_EMAIL_BODY . '","' . $a_article_email_body . '"),'.
            ' ("' . CONFIG_R_ARTICLE_EMAIL_SUB . '","' . $r_article_email_sub . '"),'.
            ' ("' . CONFIG_R_ARTICLE_EMAIL_BODY . '","' . $r_article_email_body . '"),'.
            ' ("' . CONFIG_REVIEW_FORM_POSITION . '","' . $default_form_pos . '"),'.
            ' ("' . CONFIG_REVIEW_STATUS . '","' . $default_review_status . '"),'.
            ' ("' . CONFIG_POINTS_SYSTEM . '","' . $points_system . '"),'.
            ' ("' . CONFIG_JS_POINTS_RATINGS . '","' . $jomsocial_ratings . '"),'.
            ' ("' . CONFIG_REFERRAL_SYSTEM . '","' . $referral_system . '")'.
            ' ON DUPLICATE KEY UPDATE config_value=VALUES(config_value)';
//echo $query;
        $db->setQuery($query);
        if(!$db->query()){
            $this->setError($this->_db->getErrorMsg());
            return false;
        }
		return true;
    }
    
    function getConfiguration() {
        
		$db     =& JFactory::getDBO();

        $query = 'SELECT config_name, config_value FROM '. TABLE_ACR_CONFIG;
        $db->setQuery($query);
		$rows = $db->loadObjectList();
        
        return $rows;
    }

    function setError($msg){
        $this->_error = $msg;
    }

    function getError(){
        return $this->_error;
    }
}
?>